package com.example.imageexample.Util;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.imageexample.R;
import com.example.imageexample.photodraweeview.OnPhotoTapListener;
import com.example.imageexample.photodraweeview.OnViewTapListener;
import com.example.imageexample.photodraweeview.PhotoDraweeView;
import com.example.imageexample.photoview.PhotoViewAttacher;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.drawable.AutoRotateDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by Administrator on 2016/3/30.
 */
public class LongOrGifImageView extends RelativeLayout implements View.OnClickListener{

    private LayoutInflater inflater;
    private View view;
    private Context mContext;
    private PhotoViewAttacher mAttacher;
    private String bigUrl;
    private Drawable ddefault_drawable;
    private Drawable load_drawable;
    private Drawable background_drawable;

    private PhotoDraweeView simpleDraweeView;
    private ImageView imageView;
    private Button btn_look;

    public LongOrGifImageView(Context context) {
        super(context);
        init(context);
    }

    public LongOrGifImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.item_longorpic, null);

        simpleDraweeView = (PhotoDraweeView) view.findViewById(R.id.simpleDraweeView);
        imageView = (ImageView) view.findViewById(R.id.longImage);
        btn_look = (Button) view.findViewById(R.id.btn_look);
        btn_look.setOnClickListener(this);
        mAttacher = new PhotoViewAttacher(imageView);

        ddefault_drawable = ContextCompat.getDrawable(mContext, R.mipmap.preview_default);
        load_drawable = new AutoRotateDrawable(ContextCompat.getDrawable(mContext, R.mipmap.loading_01), 1000);
        background_drawable = ContextCompat.getDrawable(mContext, R.color.black);

        addView(view);
    }

    public void setSimpleDraweeView(String big_url ,boolean needToShowBtnLook){
        this.bigUrl = big_url;
        Uri uri = null;
        if (big_url.startsWith("http")||big_url.startsWith("file://")||big_url.startsWith("res://")) {
            uri = Uri.parse("" + big_url);
        } else if (big_url.startsWith("/storage/")) {
            uri = Uri.parse("file://" + mContext.getPackageName() + "/" + big_url);
        }else{
            uri = Uri.parse("" + big_url);
        }
        Log.e("Uri","Uri:"+uri.getPath());

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(Utils.screenWidth(mContext), Utils.screenHeight(mContext)))
                        .build();
        simpleDraweeView.setController(Fresco.newDraweeControllerBuilder()
                .setOldController(simpleDraweeView.getController())
                .setImageRequest(request)
                .setAutoPlayAnimations(true)
                .setControllerListener(new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        super.onFinalImageSet(id, imageInfo, animatable);
                        simpleDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                    }
                })
                .build());
        GenericDraweeHierarchy hierarchy =
                new GenericDraweeHierarchyBuilder(mContext.getResources())
                        .setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER)
                        .setFadeDuration(300)
                        .setFailureImage(ddefault_drawable, ScalingUtils.ScaleType.CENTER_INSIDE)
                        .setProgressBarImage(load_drawable)
                        .setBackground(background_drawable)
                        .build();
        simpleDraweeView.setHierarchy(hierarchy);
        simpleDraweeView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {

            }
        });
        simpleDraweeView.setOnViewTapListener(new OnViewTapListener() {
            @Override
            public void onViewTap(View view, float x, float y) {

            }
        });

        if(needToShowBtnLook){
            btn_look.setVisibility(View.VISIBLE);
        }else{
            btn_look.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_look:
                imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                btn_look.setVisibility(View.GONE);
                simpleDraweeView.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);


                imageView.setImageResource(R.mipmap.longimg);
                mAttacher.update();
                break;
        }
    }


}
